package com.base.caselib;

import android.os.Environment;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.arvin.imagepicker.bean.ImageItem;
import com.base.caselib.bean.CarInfo;
import com.base.caselib.bean.CarType;
import com.base.caselib.databinding.ActivityVehicleupBinding;
import com.base.caselib.presenter.VehicleUpPresenter;
import com.base.caselib.view.VehicleUpView;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.utils.ToastUtil;
import com.base.commonlib.widget.CommonDialog;
import com.base.commonlib.widget.ParkCheckDialog;
import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by weikailiang on 2020/2/2.
 */
@Route(path = "/crpuct/vehicleup")
public class VehicleUpActivity extends CBaseBarMvpActivity<VehicleUpPresenter> implements VehicleUpView {
    private ActivityVehicleupBinding mBinding;

    private CarInfo mCarInfo = new CarInfo();
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_vehicleup;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityVehicleupBinding) mRootBinding;
        mPresenter = new VehicleUpPresenter(this,this);
        mPresenter.start();
        setBackNavigationTip(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWaringDialog.show();
            }
        });
        setBarTitle("车辆登记");
        mPresenter.getCarType();
        mBinding.setEntity(mCarInfo);
    }

    @Override
    protected void start() {
        mBinding.drivingFirstTimeN.setChecked(true);
        mBinding.fareN.setChecked(true);


        mBinding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CommonDialog commonDialog = new CommonDialog(mContext, "确认登记?");
                commonDialog.setOnPosClick(new CommonDialog.OnPosClick() {
                    @Override
                    public void onClick() {
                        List<String> strList = new ArrayList<>();
                        if (mBinding.carPic.getPics()!=null){
                            for (ImageItem item : mBinding.carPic.getPics()){
                                strList.add(item.path);
                            }
                        }
                        mBinding.getEntity().setCarFiles(strList);

                        List<String> strList1 = new ArrayList<>();
                        if (mBinding.comXin.getPics()!=null){
                            for (ImageItem item : mBinding.comXin.getPics()){
                                strList1.add(item.path);
                            }
                        }
                        mBinding.getEntity().setXszFiles(strList1);

                        List<String> strList2 = new ArrayList<>();
                        if (mBinding.comPic.getPics()!=null){
                            for (ImageItem item : mBinding.comPic.getPics()){
                                strList2.add(item.path);
                            }
                        }
                        mBinding.getEntity().setJszFiles(strList2);

                        List<String> strList3 = new ArrayList<>();
                        if (mBinding.cardPic.getPics()!=null){
                            for (ImageItem item : mBinding.cardPic.getPics()){
                                strList3.add(item.path);
                            }
                        }
                        mBinding.getEntity().setSfzFiles(strList3);
                        CarType carType = (CarType) mBinding.spType.getSelectedItem();
                        mBinding.getEntity().setCarType(carType.getDictValue());
                        if (!TextUtils.isEmpty(mBinding.personNum.getText().toString())) {
                            mBinding.getEntity().setPeoples(Integer.valueOf(mBinding.personNum.getText().toString()));
                        }
                        if (mBinding.drivingFirstTimeN.isChecked()){
                            mBinding.getEntity().setAreaContact(0);
                        }else {
                            mBinding.getEntity().setAreaContact(1);
                        }
                        if (mBinding.fareN.isChecked()){
                            mBinding.getEntity().setFebrileState(0);
                        }else {
                            mBinding.getEntity().setFebrileState(1);
                        }
                        if (mBinding.cardNum.getText().toString().length()>10){
                            ToastUtil.show("请输入正确的车牌号码");
                            return;
                        }

                        mPresenter.up(mBinding.getEntity());
                    }
                });
                commonDialog.show();
            }
        });
    }

    @Override
    public void returnSpinnerCarType(List<CarType> list) {
        mBinding.spType.init(list);
        String number = getIntent().getStringExtra("number");
        String color = getIntent().getStringExtra("color");
        final String path = getIntent().getStringExtra("path");
        if(!TextUtils.isEmpty(number)&&number.length()>=7)
        {
            number= number.toUpperCase();
            final String sheng = number.substring(0, 1);
            final String letter = number.substring(1, 2);
            final String other = number.substring(2, number.length());
            String type=null;
            if("蓝".equals(color))
            {
                type="小型汽车";
            }else if("黄".equals(color))
            {
                type="大型汽车";

            }else if("绿".equals(color))
            {
                type="小型新能源汽车";
            }
            if(!TextUtils.isEmpty(type))
            {
                mBinding.spType.setSelection(mPresenter.getTypePosition(type));
            }

            ParkCheckDialog parkCheckDialog = new ParkCheckDialog(mContext);
            parkCheckDialog.setNumber(number);
            parkCheckDialog.setType(type);
            final String finalNumber = number;
            parkCheckDialog.setOnPosClick(new ParkCheckDialog.OnPosClick() {
                @Override
                public void onClick() {
//                    mBinding.tvSheng.setText(sheng);
//                    mBinding.tvLetter.setText(letter);
//                    mBinding.etNum.setText(other);
                    mBinding.cardNum.setText(finalNumber);
                    int i = path.lastIndexOf("/");
                    String name=path.substring(i+1,path.length());
                    mBinding.carPic.addPic(new ImageItem(path,name,0));
                }
            });
            parkCheckDialog.setOnNegClick(new ParkCheckDialog.OnNegClick() {
                @Override
                public void onClick() {
                    File file = new File(path);
                    if(file.exists())
                    {
                        file.delete();
                    }
                    //自定义
                    ARouter.getInstance().build("/lpark/memory")
                            .withString("ResultActivity", "/crpuct/vehicleup")
                            .withString("PATH", Environment.getExternalStorageDirectory().getAbsolutePath() + "/lpark/park")
                            .navigation();
                    finish();
                }
            });
            parkCheckDialog.show();
        }else
        {
            showMes("未识别到车牌信息!");
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }
}
